package com.example.demo53.ui.Temperation;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatSeekBar;

import com.example.color3.BlurMaskCircularView;
import com.example.color3.colorpicker.ColorPicker;
import com.example.demo53.MainActivity;
import com.example.demo53.R;
import com.example.demo53.methods.net.boardcast_ip;

import static com.example.color3.colorpicker.ColorPicker.alpha;
import static com.example.color3.colorpicker.ColorPicker.blue;
import static com.example.color3.colorpicker.ColorPicker.green;
import static com.example.color3.colorpicker.ColorPicker.red;
import static com.example.color3.colorpicker.ColorPicker.vcolor;


public class a_rpgLight extends MainActivity {


    private BlurMaskCircularView mMaskFilterView;//颜色预览区域
    private ColorPicker mColorPicker;
    private AppCompatSeekBar mSeekBar1, mSeekBar2, mSeekBar3;
    private TextView tvSeekBar1, tvSeekBar2, tvSeekBar3 ,tx_argb;
    private ImageView icon_light;


    private int gray = -8355712;
    private int color_icon= gray;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.argb_controller);
        initUI();
    }

    public void initUI() {
        findView();
        initListener();

        //初始化mcolor和取色盘颜色
        mColor = mMaskFilterView.getColor();
        mColorPicker.setColor(mColor);

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                //mColorPicker.setVisibility(View.INVISIBLE);
            }
        }, 1000);
    }

    /**
     *
     */
    private void findView() {
        mMaskFilterView = findViewById(R.id.mask_view);
        mColorPicker = findViewById(R.id.color_picker);
        mSeekBar1 = findViewById(R.id.seek1);
        mSeekBar2 = findViewById(R.id.seek2);
        mSeekBar3 = findViewById(R.id.seek3);
        tvSeekBar1 = findViewById(R.id.tv_seek1);
        tvSeekBar2 = findViewById(R.id.tv_seek2);
        tvSeekBar3 = findViewById(R.id.tv_seek3);
        tx_argb=findViewById(R.id.tx_argb);
        icon_light = findViewById(R.id.icon_light);
    }

    private int mColor;//RGB颜色
    private float mHue;//色调范围0-360
    private float mSat;//饱和度范围0-1
    private float mVal;//亮度范围0-1

    @SuppressLint("SetTextI18n")
    private void showColorData(){
        tx_argb.setText(red+"\n"+green+"\n"+blue+"\n"+"alpha:"+"\t"+alpha+"\n"+vcolor+"\n");
        boardcast_ip a = new boardcast_ip();
        a.initdata_color(  mColor);
        a.start();

    }
    /**
     * 事件监听
     */
    private void initListener() {


//----------------lighting switch
        icon_light.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (color_icon== gray) {
                    icon_light.setColorFilter(color_icon);
                    color_icon = -1;
                    //send data of lighting on


                } else if(color_icon == -1){

                    icon_light.setColorFilter(color_icon);
                    color_icon = gray;
                }
            }


        });
        //取色盘提取颜色（松开手）
        mColorPicker.setOnColorSelectedListener(new ColorPicker.OnColorSelectedListener() {
            @Override
            public void onColorSelected(int color) {
                //mColorPicker.showcolor();
                mColor = color;

                showColorData();
            }
        });
        //取色盘提取颜色（不松开手）
        mColorPicker.setOnColorChangedListener(new ColorPicker.OnColorChangedListener() {
            @Override
            public void onColorChanged(int color) {
                mMaskFilterView.setColor(color);



                mColor = mMaskFilterView.getColor();

                showColorData();
//                float[] hsv = new float[3];
//                Color.colorToHSV(color, hsv);
//                mHue = hsv[0];
//                mSat = hsv[1];
//                mVal = hsv[2];
            }
        });
        //饱和度
        mSeekBar1.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean b) {
                tvSeekBar1.setText(progress + "%");
                float[] hsv = new float[3];
                Color.colorToHSV(mColor, hsv);
                mHue = hsv[0];

                //-----------------------------------------
                mSat = (float) progress / seekBar.getMax();//取百分比
                mVal = hsv[2];
                //HSVtocolor将颜色hsv转化为RGB
                mMaskFilterView.setColor(Color.HSVToColor(new float[]{mHue, mSat, mVal}));
                mColor = mMaskFilterView.getColor();
                showColorData();

            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
            }
        });
        //亮度
        mSeekBar2.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean b) {
                tvSeekBar2.setText(progress + "%");
                float[] hsv = new float[3];
                Color.colorToHSV(mColor, hsv);
                mHue = hsv[0];
                mSat = hsv[1];
                mVal = (float) progress / seekBar.getMax();
                if (mVal < 0.35) {
                    mVal = 0.35f;
                }
                mMaskFilterView.setColor(Color.HSVToColor(new float[]{mHue, mSat, mVal}));

                showColorData();
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
            }
        });


        //色调
//        mSeekBar3.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
//
//            @Override
//            public void onProgressChanged(SeekBar seekBar, int progress, boolean b) {
//                tvSeekBar3.setText(progress + "%");
//                float[] hsv = new float[3];
//                Color.colorToHSV(mColor, hsv);
//                mHue = (float) (3.6 * progress);
//                mSat = hsv[1];
//                mVal = hsv[2];
//                mMaskFilterView.setColor(Color.HSVToColor(new float[]{mHue, mSat, mVal}));
//
//                mColor = mMaskFilterView.getColor();
//                boardcast_ip a = new boardcast_ip();
//                a.initdata_color(mColor);
//                a.start();
//            }
//
//            @Override
//            public void onStartTrackingTouch(SeekBar seekBar) {
//
//            }
//
//            @Override
//            public void onStopTrackingTouch(SeekBar seekBar) {
//            }
//        });
    }

}

